package com.forever.picassodemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Picasso.LoadedFrom;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;

public class MainActivity extends Activity {

	TextView textView1;
	ImageView imageView1, imageView2, imageView3;
	String url1 = "http://www.baidu.com/img/bdlogo.png";
	String url2 = "http://www.baigoogledu.com/images/logo.png";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		textView1 = (TextView) findViewById(R.id.textView1);
		imageView1 = (ImageView) findViewById(R.id.imageView1);
		imageView2 = (ImageView) findViewById(R.id.imageView2);
		imageView3 = (ImageView) findViewById(R.id.imageView3);

		Picasso mPicasso = Picasso.with(this);
		// ImageView 常规加载图片
		mPicasso.load(url1).into(imageView1);

		// 图片的一些配置方法
		/**
		 * 1,使用Picasso自带的图片处理API
		 * resize()、centerCrop() 、centerInside()、rotate()等，可以自行尝试，
		 */
		mPicasso.load(url1).resize(150, 100).centerInside().into(imageView2);

		/**
		 * 2，自定义图片处理 CustomTransformation
		 */
		mPicasso.load(url1).transform(new CustomTransformation())
				.into(imageView2);

		/**
		 * 3，设置默认、下载失败图片
		 */
		mPicasso.load("故意定义错，造成下载失败").placeholder(R.drawable.expand)
				.error(R.drawable.ic_launcher).into(imageView3);

		// 其他控件加载图片，需要通过包装一下。(有一个疑问，使用Target：第一次无法显示出下载的图片。之后便可以，不知道为什么)
		mPicasso.load(url2).into(new ViewSetImage(textView1));

	}

	/**
	 * 
	 * 包装控件的类
	 * 当然也可以继承TextView再去实现Target接口。不过这样的话，适用性不强，只针对于TextView。
	 * @author wy
	 *
	 */
	static class ViewSetImage implements Target {
		private View view;

		public ViewSetImage(View view) {

			if (view != null) {
				this.view = view;
			}

		}

		// 下载失败，调用的方法
		@Override
		public void onBitmapFailed(Drawable mDrawable) {
			view.setBackgroundResource(R.drawable.ic_launcher);
		}

		// 下载成功，设置图片
		@SuppressWarnings("deprecation")
		@Override
		public void onBitmapLoaded(Bitmap bm, LoadedFrom mLoadedFrom) {

			view.setBackground(new BitmapDrawable(bm));
		}

		// 默认图片
		@Override
		public void onPrepareLoad(Drawable mDrawable) {
			view.setBackgroundResource(R.drawable.expand);
		}

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((view == null) ? 0 : view.hashCode());
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ViewSetImage other = (ViewSetImage) obj;
			if (view == null) {
				if (other.view != null)
					return false;
			} else if (!view.equals(other.view))
				return false;
			return true;
		}

	}

	/**
	 * 自定义图片处理
	 */
	static class CustomTransformation implements Transformation {

		@Override
		public String key() {
			return "DrawText";
		}

		@Override
		public Bitmap transform(Bitmap bitmap) {

			synchronized (CustomTransformation.class) {
				if (bitmap == null) {
					return null;
				}

				// 裁切为圆形图片
				Bitmap resultBitmap = toRoundBitmap(bitmap);

				// 记得回收
				bitmap.recycle();
				return resultBitmap;
			}
		}

	}

	/**
	 * 转换图片成圆形
	 * @param bitmap 传入Bitmap对象
	 * @return
	 */
	public static Bitmap toRoundBitmap(Bitmap bitmap) {
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();
		float roundPx;
		float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
		if (width <= height) {
			roundPx = width / 2;
			top = 0;
			bottom = width;
			left = 0;
			right = width;
			height = width;
			dst_left = 0;
			dst_top = 0;
			dst_right = width;
			dst_bottom = width;
		} else {
			roundPx = height / 2;
			float clip = (width - height) / 2;
			left = clip;
			right = width - clip;
			top = 0;
			bottom = height;
			width = height;
			dst_left = 0;
			dst_top = 0;
			dst_right = height;
			dst_bottom = height;
		}

		Bitmap output = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect src = new Rect((int) left, (int) top, (int) right,
				(int) bottom);
		final Rect dst = new Rect((int) dst_left, (int) dst_top,
				(int) dst_right, (int) dst_bottom);
		final RectF rectF = new RectF(dst);

		paint.setAntiAlias(true);

		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, src, dst, paint);
		return output;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
